home *** CD-ROM | disk | FTP | other *** search
/ Mastering Internet Develo…oft ActiveX Technologies / Mastering Internet Development with ActiveX (1996)(Microsoft).iso / labs / lab10.2 / solution / echo.cpp next >
C/C++ Source or Header  |  1996-07-16  |  5KB  |  181 lines

  1. // ECHO.CPP - Implementation file for your Internet Server
  2. //    Echo Extension
  3.  
  4. #include <afx.h>
  5. #include <afxwin.h>
  6. #include <afxisapi.h>
  7. #include "resource.h"
  8. #include "Echo.h"
  9.  
  10. ///////////////////////////////////////////////////////////////////////
  11. // command-parsing map
  12.  
  13. BEGIN_PARSE_MAP(CEchoExtension, CHttpServer)
  14.     // TODO: insert your ON_PARSE_COMMAND() and 
  15.     // ON_PARSE_COMMAND_PARAMS() here to hook up your commands.
  16.     // For example:
  17.  
  18.     //Lab 10.2, ex 2 - Implementing Echo
  19.     ON_PARSE_COMMAND(EchoRequest, CEchoExtension, ITS_PSTR)
  20.     ON_PARSE_COMMAND_PARAMS("option=full")
  21.  
  22.     ON_PARSE_COMMAND(Default, CEchoExtension, ITS_EMPTY)
  23.     DEFAULT_PARSE_COMMAND(Default, CEchoExtension)
  24. END_PARSE_MAP(CEchoExtension)
  25.  
  26.  
  27. ///////////////////////////////////////////////////////////////////////
  28. // The one and only CEchoExtension object
  29.  
  30. CEchoExtension theExtension;
  31.  
  32.  
  33. ///////////////////////////////////////////////////////////////////////
  34. // CEchoExtension implementation
  35.  
  36. CEchoExtension::CEchoExtension()
  37. {
  38. }
  39.  
  40. CEchoExtension::~CEchoExtension()
  41. {
  42. }
  43.  
  44. BOOL CEchoExtension::GetExtensionVersion(HSE_VERSION_INFO* pVer)
  45. {
  46.     // Call default implementation for initialization
  47.     CHttpServer::GetExtensionVersion(pVer);
  48.  
  49.     // Load description string
  50.     TCHAR sz[HSE_MAX_EXT_DLL_NAME_LEN+1];
  51.     ISAPIVERIFY(::LoadString(AfxGetResourceHandle(),
  52.             IDS_SERVER, sz, HSE_MAX_EXT_DLL_NAME_LEN));
  53.     _tcscpy(pVer->lpszExtensionDesc, sz);
  54.     return TRUE;
  55. }
  56.  
  57. ///////////////////////////////////////////////////////////////////////
  58. // CEchoExtension command handlers
  59.  
  60. void CEchoExtension::Default(CHttpServerContext* pCtxt)
  61. {
  62.     StartContent(pCtxt);
  63.     WriteTitle(pCtxt);
  64.  
  65.     *pCtxt << "<P><H1>Help for Echo.dll</H1><P><HR>";
  66.     EchoHelp(pCtxt);
  67.  
  68.     EndContent(pCtxt);
  69. }
  70.  
  71. //Lab 10.2, ex 2 - Implementing Echo
  72. void CEchoExtension::EchoRequest(CHttpServerContext* pCtxt, LPCTSTR pstrOption)
  73. {
  74.     StartContent(pCtxt);
  75.     WriteTitle(pCtxt);
  76.  
  77.     *pCtxt << "<P><H1>Echo.dll</H1><P><HR>";
  78.  
  79.     if ( _stricmp(pstrOption, "full") == 0)
  80.     {
  81.         EchoHead(pCtxt);
  82.         *pCtxt << "<BR>";
  83.         EchoBody(pCtxt);
  84.     }
  85.     else if ( _stricmp(pstrOption, "header") == 0)
  86.     {
  87.         EchoHead(pCtxt);
  88.     }
  89.     else if ( _stricmp(pstrOption, "body") == 0)
  90.     {
  91.         EchoBody(pCtxt);
  92.     }
  93.     else
  94.         BadSyntax(pCtxt);
  95.  
  96.     EndContent(pCtxt);
  97. }
  98.  
  99. ///////////////////////////////////////////////////////////////////////
  100. // Implementation member functions
  101. //Lab 10.2, ex 2 - Implementing Echo
  102.  
  103. void CEchoExtension::EchoHead(CHttpServerContext* pCtxt)
  104. {
  105.     char pstrBuffer[1000];
  106.     DWORD dwSize = sizeof(pstrBuffer);
  107.  
  108.     *pCtxt << "<P><H3>Request Header Information</H3>";
  109.     *pCtxt << "<BR>Request method: " << pCtxt->m_pECB->lpszMethod;
  110.     *pCtxt << "<BR>Tranlsated path: " << pCtxt->m_pECB->lpszPathTranslated;
  111.     
  112.     pCtxt->GetServerVariable("QUERY_STRING", pstrBuffer, &dwSize);
  113.     *pCtxt << "<BR>Query String: " << pstrBuffer;
  114.  
  115.     dwSize = sizeof(pstrBuffer); pstrBuffer[0] = 0;
  116.     pCtxt->GetServerVariable("REMOTE_ADDR", pstrBuffer, &dwSize);
  117.     *pCtxt << "<BR>Client IP address: " << pstrBuffer;
  118.  
  119. }
  120.  
  121. void CEchoExtension::EchoBody(CHttpServerContext* pCtxt)
  122. {
  123.     char pstrBuffer[4000];
  124.     DWORD dwSize = sizeof(pstrBuffer);
  125.  
  126.     *pCtxt << "<P><H3>Request Body Information</H3><BR>";
  127.  
  128.     BOOL b = pCtxt->ReadClient(pstrBuffer, &dwSize);
  129.     *pCtxt << "Body is " << (long int)dwSize << " bytes <BR>" ;
  130.     if (b == TRUE)
  131.         *pCtxt << pstrBuffer;
  132. }
  133.  
  134.  
  135. void CEchoExtension::BadSyntax(CHttpServerContext* pCtxt)
  136. {
  137.     *pCtxt << "<H3>Bad Syntax Error!</H3><P>";
  138.     *pCtxt << "Proper syntax is as follows:";
  139.     EchoHelp(pCtxt);
  140. }
  141.  
  142. void CEchoExtension::EchoHelp(CHttpServerContext* pCtxt)
  143. {
  144.     *pCtxt << "<P><I>Supported method:</I>";
  145.     *pCtxt << "<BR><B><TT>EchoRequest</TT></B> - currently the only method supported."
  146.            << " Dynamically creates an HTML page containing HTTP request information.";
  147.     *pCtxt << "<P><I>Supported arguments:</I>";
  148.     *pCtxt << "<BR><B><TT>Full</TT></B> - echo back the entire HTTP request message.";
  149.     *pCtxt << "<BR><B><TT>Body</TT></B> - echo back only the HTTP request message body.";
  150.     *pCtxt << "<BR><B><TT>Header</TT></B> - echo back only the HTTP request message head.";
  151. }
  152.  
  153. ///////////////////////////////////////////////////////////////////////
  154. // If your extension will not use MFC, you'll need this code to make
  155. // sure the extension objects can find the resource handle for the
  156. // module.  If you convert your extension to not be dependent on MFC,
  157. // remove the comments arounn the following AfxGetResourceHandle()
  158. // and DllMain() functions, as well as the g_hInstance global.
  159.  
  160. /****
  161.  
  162. static HINSTANCE g_hInstance;
  163.  
  164. HINSTANCE AFXISAPI AfxGetResourceHandle()
  165. {
  166.     return g_hInstance;
  167. }
  168.  
  169. BOOL WINAPI DllMain(HINSTANCE hInst, ULONG ulReason,
  170.                     LPVOID lpReserved)
  171. {
  172.     if (ulReason == DLL_PROCESS_ATTACH)
  173.     {
  174.         g_hInstance = hInst;
  175.     }
  176.  
  177.     return TRUE;
  178. }
  179.  
  180. ****/
  181.